﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_Retirements : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IRetirementBLL retireBLL = new RetirementBLL();
        private IJobSiteBLL jsBLL = new JobSiteBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private RadComboBox cmbEmployeeType, cmbRetireYear, cmbRetireStatus;
        private object _dataItem = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            cmbEmployeeType = (RadComboBox)cbEmployeeType.FindControl("cbEmployeeType");
            cmbRetireYear = (RadComboBox)cbYear.FindControl("cbYear");
            cmbRetireStatus = (RadComboBox)cbRecordStatus.FindControl("cbRecordStatus");
            FillDataToApprover();
            FillDataToJobSite();           

            if (DataItem != null)
            {
                cbEmployeeType.ListEmployeeTypeComboBox();
                cbYear.ListYear();
                cbRecordStatus.ListStatusCommandComboBox();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblRetireSeq.Text = DataBinder.Eval(DataItem, "retire_seq").ToString();
                    cmbEmployeeType.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "emp_type_seq"));
                    cmbRetireStatus.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "retire_status"));
                    cmbJobSite.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "job_site_seq"));
                    cmbRetireYear.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "retire_year"));
                    txtStartDate.SetValue(DataBinder.Eval(DataItem, "start_date", "{0:dd/MM/yyyy}").ToString());
                   // cmbEmployee.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "EMP_SEQ"));
                    //txtRefNo.Text = DataBinder.Eval(DataItem, "RefNo").ToString(); edit by mai
                    //txtRetireDescr.Text = DataBinder.Eval(DataItem, "Description").ToString();
                    txtApprovedDate.SetValue(DataBinder.Eval(DataItem, "approved_date", "{0:dd/MM/yyyy}").ToString());                    
                    //txtApprovedName.Text = DataBinder.Eval(DataItem, "ApprovedName").ToString();
                    //txtApprovedPosition.Text = DataBind er.Eval(DataItem, "ApprovedPosition").ToString();
                    //txtRemarks.Text = DataBinder.Eval(DataItem, "Remarks").ToString();
                }
                else
                {
                    cmbRetireYear.SelectedValue = Convert.ToString(Int32.Parse(DateTime.Today.Year.ToString()) + 543);
                }
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void FillDataToJobSite()
        {
            IList<JobSiteInfo> InfoList = jsBLL.ListJobSiteByTypes(SystemInfo._JobsiteExternal, "2");
            cmbJobSite.DataSource = InfoList;
        }
        public void FillDataToApprover()
        {
            DataSet ds = empBLL.LoadEmployeeForApproveCommands();
            DataTable dt = ds.Tables[0];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                RadComboBoxItem item = new RadComboBoxItem();

                item.Text = dt.Rows[i]["EMP_NAME"].ToString();
                item.Value = dt.Rows[i]["EMP_SEQ"].ToString();

                item.Attributes.Add("EMP_NAME", dt.Rows[i]["EMP_NAME"].ToString());
                item.Attributes.Add("EXEC_POS_NAME", dt.Rows[i]["EXEC_POS_NAME"].ToString());

                cmbEmployee.Items.Add(item);
                item.DataBind();
            }
        }
        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            txtAppName.Enabled = false;
            cmbEmployee.Enabled = true;
            cmbEmployee.ClearSelection();
            cmbEmployee.Text = string.Empty;
            txtAppName.Text = "";
        }
        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            txtAppName.Enabled = true;
            cmbEmployee.Enabled = false;

            cmbEmployee.ClearSelection();
            cmbEmployee.Text = string.Empty;
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            retireBLL.Insert(cmbEmployeeType.SelectedValue
                , Convert.ToInt32(cmbRetireYear.SelectedValue)
                , txtStartDate.GetStringValue().Length > 0 ? Convert.ToDateTime(txtStartDate.GetStringValue()) : (DateTime?)null
                , cmbJobSite.SelectedValue
                , txtRefNo.Text
                , txtTitle.Text.Trim()
                , txtRetireDescr.Text
                , txtAppName.Text
                , txtAppPosition.Text
                , txtApprovedDate.GetStringValue().Length > 0 ? Convert.ToDateTime(txtApprovedDate.GetStringValue()) : (DateTime?)null
                , cmbRetireStatus.SelectedValue
                , txtRemarks.Text
                , ssMng.EmpSeqI);
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string Approvider = "";

            if (rdoApptypeIn.Checked == true)
            {
                Approvider = cmbEmployee.SelectedValue;
            }
            else
            {
                Approvider = txtAppName.Text;

            }
            retireBLL.Update(Convert.ToInt32(lblRetireSeq.Text)
                , cmbEmployeeType.SelectedValue
                , Convert.ToInt32(cmbRetireYear.SelectedValue)
                , txtStartDate.GetStringValue().Length > 0 ? Convert.ToDateTime(txtStartDate.GetStringValue()) : (DateTime?)null
                , cmbJobSite.SelectedValue
                , txtRefNo.Text
                , txtTitle.Text.Trim()
                , txtRetireDescr.Text
                , Approvider
                , txtAppPosition.Text
                , txtApprovedDate.GetStringValue().Length > 0 ? Convert.ToDateTime(txtApprovedDate.GetStringValue()) : (DateTime?)null
                , cmbRetireStatus.SelectedValue
                , txtRemarks.Text.Trim()
                , ssMng.EmpSeqI);
        }

        protected void cmbEmployee_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
   
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            
        }
    }
}
